using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.ComponentModel;

namespace Mdv.wssAddins.wss
{
    public class wssMetadata : Model
    {
        #region Non-Designer Properties
        private wssContentTypes _contentTypes;
        [Browsable(false)]
        public wssContentTypes ContentTypes
        {
            get { return _contentTypes; }
        }
        private wssFields _fields;
        [Browsable(false)]
        public wssFields Fields
        {
            get { return _fields; }
        }
        private wssForms _forms;
        [Browsable(false)]
        public wssForms Forms
        {
            get { return _forms; }
        }
        private wssViews _views;
        [Browsable(false)]
        public wssViews Views
        {
            get { return _views; }
        }
        #endregion
        public override string ToString()
        {
            return "Metadata";
        }
        public override void ConstructMap()
        {
        }
        public override void LoadChildren(XmlNode node)
        {
            foreach (XmlNode xn in node.ChildNodes)
            {
                switch (xn.Name)
                {
                    case "ContentTypes":
                        wssContentTypes contentTypes = new wssContentTypes();
                        contentTypes.Load(xn);
                        Children.Add(contentTypes);
                        break;
                    case "Fields":
                        wssFields fields = new wssFields();
                        fields.Load(xn);
                        Children.Add(fields);
                        break;
                    case "Views":
                        wssViews views = new wssViews();
                        views.Load(xn);
                        Children.Add(views);
                        break;
                    case "Forms":
                        wssForms forms = new wssForms();
                        forms.Load(xn);
                        Children.Add(forms);
                        break;
                }
            }
        }
        public wssMetadata()
        {
            Actions = new List<Action>();
            Actions.Add(new Action("Add ContentTypes", this.AddContentTypes));
            Actions.Add(new Action("Add Fields", this.AddFields));
            Actions.Add(new Action("Add Views", this.AddViews));
            Actions.Add(new Action("Add Forms", this.AddForms));
            Actions.Add(new Action("Delete", this.Delete));
        }
        public void AddContentTypes(ModelEventArgs e)
        {
            bool found = false;
            foreach (Model m in Children)
                if (m is wssContentTypes)
                {
                    found = true;
                    break;
                }
            if(!found) NodeAdded(new wssContentTypes(), "ContentTypes");
        }
        public void AddFields(ModelEventArgs e)
        {
            bool found = false;
            foreach (Model m in Children)
                if (m is wssFields)
                {
                    found = true;
                    break;
                }
            if (!found) NodeAdded(new wssFields(), "Fields");
        }
        public void AddViews(ModelEventArgs e)
        {
            bool found = false;
            foreach (Model m in Children)
                if (m is wssViews)
                {
                    found = true;
                    break;
                }
            if (!found) NodeAdded(new wssViews(), "Views");
        }
        public void AddForms(ModelEventArgs e)
        {
            bool found = false;
            foreach (Model m in Children)
                if (m is wssForms)
                {
                    found = true;
                    break;
                }
            if (!found) NodeAdded(new wssForms(), "Forms");
        }
        public void Delete(ModelEventArgs e)
        {
            NodeDeleted("Metadata");
        }
    }
}